//
//  Marcos.h
//  SDK
//
//  Created by ligq on 2020/4/10.
//  Copyright © 2020 synjones. All rights reserved.
//

#ifndef Marcos_h
#define Marcos_h

// app version key
#define kApplicationVersionKey   @"SBApplicationVersionKey"
// app name
#define kAppName    ([[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"])
// app versino
#define kAppVersion ([[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"])
// app build
#define kAppBundleId   ([[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"])
// Get dispatch_get_main_queue()
#define kMainThread (dispatch_get_main_queue())
// Get default dispatch_get_global_queue
#define kGlobalThread dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
// Radians convert to degress.
#define kRadiansToDegrees(radians) ((radians) * (180.0 / M_PI))
// Degrees convert to randians.
#define kDegreesToRadians(angle) ((angle) / 180.0 * M_PI)
// Fast to get iOS system version
#define kIOSVersion ([UIDevice currentDevice].systemVersion.floatValue)
// More fast way to get app delegate
#define kAppDelegate ((AppDelegate *)[[UIApplication  sharedApplication] delegate])

#define kAppWindow    [UIApplication sharedApplication].delegate.window

#define kRootViewController [UIApplication sharedApplication].delegate.window.rootViewController

#define kLoadBundle  @"loading.bundle"

#pragma mark - Device Frame
// Get the screen's height.
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
// Get the screen's width.
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
// Get the screen's bounds.
#define kScreenBounds ([UIScreen mainScreen].bounds)


//判断机型
#define IS_IPhone4_less kScreenHeight <  568.0
#define IS_IPhone5      kScreenHeight == 568.0
#define IS_iPhone6      kScreenHeight == 667.0
#define IS_iPhone6P     kScreenHeight == 736.0
#define IS_iPhoneX      sh_IsIPhoneXSeries()

// status bar height.
#define kStatusBarHeight (IS_iPhoneX ? 44.f : 20.f)
// Navigation bar height.
#define kNavigationBarHeight  (IS_iPhoneX ? 88.f : 64.f)
// Tab bar height.
#define kTabbarHeight (IS_iPhoneX ? 88.f : 49.f)
// Tabbar safe bottom margin.
#define kBarSafeHeight  (IS_iPhoneX ? 34.f : 0.f)
// Status bar & navigation bar height.
#define kStatusBarAndNavigationBarHeight (IS_iPhoneX ? 90.f : 66.f)

#define kNavBarSafeHeight kStatusBarAndNavigationBarHeight

static inline BOOL sh_IsIPhoneXSeries() {
    BOOL tmpiPhoneXSeries = NO;
    if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
        return tmpiPhoneXSeries;
    }
    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            tmpiPhoneXSeries = YES;
        }
    }
    return tmpiPhoneXSeries;
}

// weakSelf
#define WeakSelf __weak typeof(self) weakSelf = self;
// strongSelf
#define StrongSelf __strong typeof(self) strongSelf = weakSelf;

#pragma mark - Judge

// Judge whether it is an empty string.
#define kIsEmptyString(s) (s == nil || [s isKindOfClass:[NSNull class]] || ([s isKindOfClass:[NSString class]] && s.length == 0))

// Judge whether it is a nil or null object.
#define kIsEmptyObject(obj) (obj == nil || [obj isKindOfClass:[NSNull class]])

// Judge whether it is a vaid dictionary.
#define kIsDictionary(objDict) (objDict != nil && [objDict isKindOfClass:[NSDictionary class]])

// Judge whether it is a valid array.
#define kIsArray(objArray) (objArray != nil && [objArray isKindOfClass:[NSArray class]])

// Judge whether the device it is ipad.
#define kIsIPad \
([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)]\
&& [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)

// Judge whether current orientation is landscape.
#define kIsLandscape (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))

#pragma mark - Generate Color

// color
///< format：0xFFFFFF
#define k16RGBColor(rgbValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0 \
green:((float)((rgbValue & 0xFF00) >> 8)) / 255.0 \
blue:((float)(rgbValue & 0xFF)) / 255.0 alpha:1.0]

#define k16RGBAlphaColor(rgbValue, a) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0 \
green:((float)((rgbValue & 0xFF00) >> 8)) / 255.0 \
blue:((float)(rgbValue & 0xFF)) / 255.0 alpha:(a)]

///< format：22,22,22
#define kRGBColor(r, g, b) ([UIColor colorWithRed:(r) / 255.0  \
green:(g) / 255.0  \
blue:(b) / 255.0  \
alpha:1])

///< format：22,22,22,0.5
#define kRGBAColor(r, g, b, a) ([UIColor colorWithRed:(r) / 255.0  \
green:(g) / 255.0  \
blue:(b) / 255.0  \
alpha:(a)])

// More easy way to use hex color to generate color.
#define kHexRGB(rgb) k16RGBColor(rgb)

// More easy way to generate a color object.
#define kRGB(r,g,b) kRGBColor(r,g,b)

// More easy way to generate a color object with rgb and alpha.
#define kRGBA(r,g,b,a) kRGBAColor(r, g, b, a)

#pragma mark - color

#define kBlackColor       [UIColor blackColor]
#define kDarkGrayColor    [UIColor darkGrayColor]
#define kLightGrayColor   [UIColor lightGrayColor]
#define kWhiteColor       [UIColor whiteColor]
#define kRedColor         [UIColor redColor]
#define kBlueColor        [UIColor blueColor]
#define kGreenColor       [UIColor greenColor]
#define kCyanColor        [UIColor cyanColor]
#define kYellowColor      [UIColor yellowColor]
#define kMagentaColor     [UIColor magentaColor]
#define kOrangeColor      [UIColor orangeColor]
#define kPurpleColor      [UIColor purpleColor]
#define kBrownColor       [UIColor brownColor]
#define kClearColor       [UIColor clearColor]
#define kRandomColor    KRGBColor(arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0)
#define kBlackOpaqueColor kHexColor(0, 0, 0, 0.5)

#pragma mark - System Singletons

// More easy way to get user default object.
#define kUserDefaults [NSUserDefaults standardUserDefaults]

// More easy way to get NSNotificationCenter object.
#define kNotificationCenter  [NSNotificationCenter defaultCenter]

// More easy way to get [NSFileManager defaultManager]
#define kFileManager [NSFileManager defaultManager]

// More easy way to post a notification from notification center.
#define kPostNotificationWithName(notificationName) \
[kNotificationCenter postNotificationName:notificationName object:nil userInfo:nil]

// More easy way to post a notification with user info from notification center.
#define kPostNotificationWithNameAndUserInfo(notificationName, userInfo) \
[kNotificationCenter postNotificationName:notificationName object:nil userInfo:userInfo]

#pragma mark - Log
// 输出日志 (格式: [时间] [哪个方法] [哪行] [输出内容])
#ifdef DEBUG
#define NSLog(format, ...)  printf("\n[%s] %s [第%d行] 💕 %s\n", __TIME__, __FUNCTION__, __LINE__, [[NSString stringWithFormat:format, ##__VA_ARGS__] UTF8String]);
#else

#define NSLog(format, ...)

#endif
// 打印方法
#define kLogFunc NSLog(@"%s", __func__)
// 打印请求错误信息
#define kLogError(error) NSLog(@"Error: %@", error)
// 销毁打印
#define kDealloc NSLog(@"\n =========+++ %@  销毁了 +++======== \n",[self class])

#define kLogLastError(db) NSLog(@"lastError: %@, lastErrorCode: %d, lastErrorMessage: %@", [db lastError], [db lastErrorCode], [db lastErrorMessage]);

#pragma mark - simplify简化
//View 圆角和加边框
#define ViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]

// View 圆角
#define ViewRadius(View, Radius)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES]

//字体
#define kFont(FONTSIZE)              [UIFont systemFontOfSize:FONTSIZE]

#define kNameFont(NAME, FONTSIZE)    [UIFont fontWithName:(NAME) size:(FONTSIZE)]

#define kBoldSysFont(FONTSIZE)       [UIFont boldSystemFontOfSize:FONTSIZE]

// 拼接字符串
#define NSStringFormat(format,...)   [NSString stringWithFormat:format,##__VA_ARGS__]
// Integer转String
#define kStringFromInteger(i)        [NSString stringWithFormat:@"%@",@(i)]
// Int转String
#define kStringFromInt(i)            [NSString stringWithFormat:@"%@",@(i)]
// 加载图片
#define kImageName(name)             [UIImage imageNamed:name]

//单例类
#define SINGLETON_FOR_HEADER(className) \
\
+ (className *)shared##className;

#define SINGLETON_FOR_CLASS(className) \
\
+ (className *)shared##className { \
static className *shared##className = nil; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
shared##className = [[self alloc] init]; \
}); \
return shared##className; \
}

#pragma mark - 适配
#define kRealValue(R) ((R)*(kScreenWidth/375.0f))
#define kScaleX       kScreenWidth / 375
#define kScaleY       kScreenHeight / 667

//字体适配
#define FONT_SIZE(size) [UIFont systemFontOfSize:FontSize(size)]
static inline CGFloat FontSize(CGFloat fontSize){
    if (kScreenWidth==320) {
        return fontSize-2;
    }else if (kScreenWidth==375){
        return fontSize;
    }else{
        return fontSize+2;
    }
}

#endif /* Marcos_h */
